package com.example.common.config;

/**
 * @author FanFu
 * @version 1.0
 * @date 2025/11/8 20:55
 */

import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaAdmin;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class KafkaTopicConfig {

    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Value("${app.kafka.topics.user-register}")
    private String userRegisterTopic;

    @Value("${app.kafka.topics.user-update}")
    private String userUpdateTopic;

    @Value("${app.kafka.topics.order-create}")
    private String orderCreateTopic;

    @Bean
    public KafkaAdmin kafkaAdmin() {
        Map<String, Object> configs = new HashMap<>();
        configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        return new KafkaAdmin(configs);
    }

    @Bean
    public NewTopic userRegisterTopic() {
        return new NewTopic(userRegisterTopic, 3, (short) 1);
    }

    @Bean
    public NewTopic userUpdateTopic() {
        return new NewTopic(userUpdateTopic, 3, (short) 1);
    }

    @Bean
    public NewTopic orderCreateTopic() {
        return new NewTopic(orderCreateTopic, 3, (short) 1);
    }
}